import React from 'react'
import { Route, Redirect } from 'react-router-dom'
import {connect} from 'react-redux'

class AuthorizedRoute extends React.Component{
	render(){
		const {component:Component,...rest}=this.props;   //对象解构
		const user=JSON.parse(window.localStorage.getItem("yuntu_index_user"));
		const isSignedIn=user && user.back1;
		if(!isSignedIn){
			this.props.dispatch({
				type:"signin_redirect_event",
				payload:this.props.location.pathname
			})
		}
		return(
			<Route {...rest} render={(props)=>{
				return isSignedIn ? <Component {...props} /> : <Redirect to="/login" />
			}} />
		)
	}
}

export default connect()(AuthorizedRoute)